package engine.util.array;

public class ModificationList<T> {
	private ListEntry<T> root;
	private int size;
	
	public void add(T t) {
		if(root == null) {
			root = new ListEntry<T>(t);
			size++;
		} else {
			ListEntry<T> next = root;
			
			while(next != null) {
				if(next.getValue() == t) {
					return;
				}
				
				if(next.getNext() == null) {
					next.setNext(new ListEntry<T>(t));
					size++;
					break;
				}
				
				
				next = next.getNext();
			}
		}
	}
	
	public void remove(T t) {
		ListEntry<T> next = root;
		ListEntry<T> prev = null;
		
		while(next != null) {
			if(next.getValue() == t) {
				if(prev != null) {
					prev.setNext(next.getNext());
				} else {
					root = next.getNext();
				}
				size--;
			}
			
			prev = next;
			next = next.getNext();
		}
	}
	
	public void loop(Looper<T> looper) {
		ListEntry<T> next = root;
		
		while(next != null) {
			looper.loop(next.getValue());
			next = next.getNext();
		}
	}
	
	public boolean contains(T t) {
		ListEntry<T> next = root;
		
		while(next != null) {
			if(next.getValue() == t) {
				return true;
			}
			next = next.getNext();
		}
		
		return false;
	}
	
	public void clear() {
		root = null;
		size = 0;
	}
	
	public int size() {
		return size;
	}
}
